﻿using Helper;
using Helper.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace Starehe.Testing
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void InitGlobalVar()
        {
            if (string.IsNullOrWhiteSpace(Helper.Properties.Settings.Default.ServerName))
                Helper.Properties.Settings.Default.ServerName = Environment.MachineName +
                    "\\Starehe";
            Helper.Properties.Settings.Default.CurrentUser = new DbUserInfo();
            if (string.IsNullOrWhiteSpace(Helper.Properties.Settings.Default.DBName))
                Helper.Properties.Settings.Default.DBName = "Starehe";
            if (string.IsNullOrWhiteSpace(Helper.Properties.Settings.Default.MostRecentBackup))
                Helper.Properties.Settings.Default.MostRecentBackup = "";
            ConnectionStringHelper.Initialize("sa", Security.Protect("000002"));
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-GB");

            Helper.Properties.Settings.Default.PropertyChanged += (o, e) =>
            {
                Helper.Properties.Settings.Default.Save();
            };
        }

        private StartUp StartUpCheck()
        {
            if ((SystemParameters.PrimaryScreenHeight < 768) || (SystemParameters.PrimaryScreenWidth < 1024))
            {
                MessageBox.Show("The screen resolution is too low.\r\nSome elements may not display properly." +
                    "\r\nPlease configure the Display setting" +
                    " to a resolution equal to or higher than 1024 x 768.", "Error",
                    MessageBoxButton.OK, MessageBoxImage.Error);
            }

            StartUp start = new StartUp();
            FileHelper.CheckFiles();
            if (Helper.Properties.Settings.Default.UseDefaultSQLInstance)
                start.IsSQLInstalled = DatabaseDefn.CheckSQLInstalled() && DatabaseDefn.CheckInstanceInstalled();
            else
            {
                start.IsSQLInstalled = DatabaseDefn.CheckSQLInstalled();
                start.IsSQLInstanceInstalled = DatabaseDefn.CheckInstanceInstalled();
            }
            start.IsDefaultLoginOK = DatabaseDefn.CheckDefaultLogin();
            start.IsDbOK = DatabaseDefn.CheckDB();

            if (Helper.Properties.Settings.Default.DoFullStartupCheck)
                start.IsDbObjectsOK = DatabaseDefn.CheckDBObjects();
            else
                start.IsDbObjectsOK = true;
            if (!start.IsSQLInstalled)
            {
                start.IsSQLInstanceInstalled = false;
                start.IsDefaultLoginOK = false;
                start.IsDbOK = false;
                start.IsDbObjectsOK = false;
                return start;
            }

            if (!start.IsSQLInstanceInstalled)
            {
                start.IsDefaultLoginOK = false;
                start.IsDbOK = false;
                start.IsDbObjectsOK = false;
                return start;
            }

            if (!start.IsDefaultLoginOK)
            {
                start.IsDbOK = false;
                start.IsDbObjectsOK = false;
                return start;
            }

            if (!start.IsDbOK)
            {
                start.IsDbObjectsOK = false;
                return start;
            }

            return start;
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            InitGlobalVar();
            MainWindow m = new MainWindow();
            m.Show();

        }

        private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
        }

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            ConnectionStringHelper.Release();
        }

        private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)
        {
            Helper.ConnectionStringHelper.Release();
        }        
    }
}
